/* **************************************************************************/
/*COPYRIGHT CCWIN-IN ALL RIGHTS RESERVED. THIS IS AN UNPUBLISHED WORK.      */
/*V8:ConvertMode=Report                                                     */
/*V8:WebEnabled=Yes                                                         */
/*V8:RunMode=Character,Windows                                              */
/*EXTRACTING DATA FROM MFG/PRO                                              */
/****************************************************************************/
/* PROCEDURE NAME        : xxcrpoapi.p                                      */
/* PROCEDURE TYPE        : PO Load CIM-LOAD Process                         */
/* DESCRIPTION           : PO Load CIM-LOAD Process                         */
/*                                                                          */     
/* INCLUDE FILES         :                                                  */
/* CALLED BY             :                                                  */
/* CALLED PROCEDURES     :                                                  */
/****************************************************************************/
/* CREATED BY            : Gaoyan                               01-Jan-24   */
/****************************************************************************/ 
/**   **/
/**   **/
/****************************************************************************/

{us/mf/mfdtitle.i}
{us/bbi/gplabel.i}
{us/wb/wbrp01.i}

{xxjson.i}
{xxcimload.i}


&SCOPED-DEFINE TimeStamp1 substring(string(year(today), "9999"),3) + string(month(today), "99") + string(day(today), "99")
&SCOPED-DEFINE TimeStamp2 substring(string(time, "HH:MM:SS"), 1, 2) + substring(string(time, "HH:MM:SS"), 4, 2) + substring(string(time, "HH:MM:SS"), 7, 2)

define input parameter vc_inputjson             as character format "x(60)" no-undo.
define input parameter vc_outputjson            as character format "x(60)" no-undo.
define input parameter vc_import                as logical  no-undo.
// define variable viReturn                     as integer     no-undo.
// define variable ilReturnDataset              as logical     no-undo.
// define variable ilPartialUpdate              as logical     no-undo.
// define variable icPartialUpdateExceptionList as character   no-undo.
// define variable ocPrimaryKey                 as character   no-undo.
// define variable ocRowid                      as character   no-undo.
// define variable oiDraftInstance              as integer     no-undo.
// define variable ocPrimaryKeyName             as character   no-undo.
// define variable vho                          as handle.
// define variable oiReturnStatus               as integer     no-undo.

define variable l_file                       as character format "x(40)" no-undo.
define variable l_line                       as character   no-undo.


define variable l_error                      as character      no-undo.
define stream f_log.
define stream f_cim.
define variable v_quote               as character initial """"   no-undo.
define variable v_default             as character initial "-"    no-undo.
define variable v_space               as character initial " "    no-undo.
define variable v_log                 as character                no-undo.
define variable v_cim                 as character                no-undo.
define variable v_fline               as character                no-undo.
define variable v_errorstatus         as logical                  no-undo.
define variable v_errstring           as character                no-undo.
define variable v_toterrorstatus      as logical initial yes      no-undo.
define variable v_status              as character                no-undo.
define variable v_existpod            as logical   initial no     no-undo.
define variable v_existpo             as logical   initial no     no-undo.
define variable v_poopen              as logical   initial no     no-undo.
define variable v_podopen             as logical   initial no     no-undo.
define variable vc_cip                as logical   initial no     no-undo.

define variable vi_filecount          as integer                  no-undo.
define variable vc_filename           as character                no-undo.
define variable vc_topath             as character                no-undo.
define variable v_runproagram        as character                no-undo.
define variable vc_traceid            as character                no-undo.


define temp-table ttpomstr no-undo

            field po_nbr         like   po_mstr.po_nbr
            field po_vend        like   po_mstr.po_vend 
            field po_site        like   po_mstr.po_site  
            field po_domain      like   po_mstr.po_domain 
            field po_ship        like   po_mstr.po_ship
            field po_bill        like   po_mstr.po_bill  
            field po_ord_date    like   po_mstr.po_ord_date 
            field po_due_date    like   po_mstr.po_due_date
            field po_start       as   date
            field po_expire      as   date
            field po_buyer       like   po_mstr.po_buyer
            field po_contact     like   po_mstr.po_contact
            field po_pr_list2    like   po_mstr.po_pr_list2
            field po_pr_list     like   po_mstr.po_pr_list2
            field po_confirm     like   po_mstr.po_confirm
            field po_sched       like   po_mstr.po_sched
            field po_type        like   po_mstr.po_type
            field po_taxable     like   po_mstr.po_taxable
            field po_taxc        like   po_mstr.po_taxc 
            field po_cr_terms    like   po_mstr.po_cr_terms
            field po_error       as  character
            field po_dataid      as  character
            field po_traceid     as  character
            field po_create_date as date
                        
            index ptsk po_dataid  
            index ponbr po_domain po_nbr .


define temp-table ttpoddet no-undo

            field pod_nbr     like   pod_det.pod_nbr
            field pod_domain  like   pod_det.pod_domain
            field pod_line    like   pod_det.pod_line
            field pod_site    like   pod_det.pod_site
            field pod_part    like   pod_det.pod_part
            field pod_qty_ord like   pod_det.pod_qty_ord
            field pod_um      like   pod_det.pod_um
            field pod_um_conv like   pod_det.pod_um_conv
            field pod_type    like   pod_det.pod_type
            field pod_pr_list like   pod_det.pod_pr_list
            field pod_fix_pr  like   pod_det.pod_fix_pr
            field pod_taxable like   pod_det.pod_taxable 
            field pod_taxc    like   pod_det.pod_taxc
            field pod_expire  like   pod_det.pod_expire
            field pod_start   as     date
            field pod_wo_lot  like   pod_det.pod_wo_lot
            field pod_op      like   pod_det.pod_op
            field pod_loc     like   pod_det.pod_loc
            field pod_acct    like   pod_det.pod_acct
            field pod_sub     like   pod_det.pod_sub
            field pod_cc      like   pod_det.pod_cc
            field pod_project like   pod_det.pod_project
            field pod_desc    like   pod_det.pod_desc
            field pod_cipid   as     character
            field pod_error   as     character  
            field pod_dataid  as     character
                        
            index podnbrline pod_domain pod_nbr pod_line pod_part
            index podtsk   pod_dataid.

   DEFINE DATASET dsDescreteOrder FOR ttpomstr, ttpoddet
            DATA-RELATION PoNbr FOR ttpomstr, ttpoddet RELATION-FIELDS(po_nbr,pod_nbr).
               
            hdataset = dataset dsDescreteOrder:HANDLE.             


mainloop:
repeat on error undo mainloop, retry mainloop:
   /* Output and Batch Input */
   
   {xxworkpath_e.i &outbound=YES &inbound=yes &archive=YES}
   
   assign
      m_outpath  = m_outpath + "po/"
      m_archpath = m_archpath + "po/"
      m_successpath = m_successpath + "po/"
      m_errorpath = m_errorpath + "po/"
      m_workpath = m_workpath + "po/".
   
 
      empty temp-table ttpoddet.
      empty temp-table ttpomstr.

   v_runproagram = "".
   vc_traceid = "".
   
   /* Load item master data from JSON  */ 
   vc_outputjson = replace(vc_inputjson,".json",".run.out.json").   
   run loadjsonfile(input vc_inputjson,
                    input vc_outputjson,
                    output vl_RetOK).
   if vl_RetOK then
   do:
      find first ttpomstr no-error.
      assign vc_traceid = ttpomstr.po_traceid.
   end.
   
    /* 校验订单号 */
    /* 校验订单号存在的情况下变更了供应商代码 */
    /* 校验行号 */
    /* 校验零件号 */
    /* 校验零件状态 */
    /* 校验订单类型 */
    /* 校验价格单 */
    /* 检查采购控制文件是否锁定 */
    /* 检查订单计量单位与零件基本计量单位是否一样 */
    /* 转包订单的工单号和工序 */
    /* 货币 */
    /* 税率相关 */

    /* Create CIM-LOAD file and do cim-load */
   v_cim = string(vc_traceid).
   v_log = string(vc_traceid).
   
  output to /home/mfg/work/cim/xxpoapi.log  .   
    
    
   if vc_import and vl_RetOK then 
   do:      
 
   /* scenario 1 order type 1.1 scheduled order or 1.2 discrete order */
   /* scenario 2 new po 1.1 domestic 1.2 import  */
   /* scenario 3 po type 3.1 normal 3.2 subcontract 3.3 memo  */
   /* scenario 4 update po 4.1 po closed 4.2 po open line closed 4.3 po open line open */
   /* I didn't find control field 
   find rqf_ctrl no-lock no-error.
   if available rqf_ctrl then 
   */
    cimloadprocess:
   do transaction on error undo cimloadprocess, leave cimloadprocess: 
   
   for each ttpomstr break by ttpomstr.po_dataid.
    
      v_cim = m_workpath + "PO" + "_" + ttpomstr.po_nbr + "_" + string(ttpomstr.po_traceid) + "_" + string(ttpomstr.po_dataid) + "_" + {&TimeStamp1} + "_" + {&TimeStamp2}  + ".cim".
      v_log = m_workpath + "PO" + "_" + ttpomstr.po_nbr + "_" + string(ttpomstr.po_traceid) + "_" + string(ttpomstr.po_dataid) + "_" + {&TimeStamp1} + "_" + {&TimeStamp2}  + ".log".
      output stream f_cim to value(v_cim).
      find po_mstr where po_mstr.po_domain = global_domain
                     and po_mstr.po_nbr = ttpomstr.po_nbr
                     no-lock no-error.
          
          /* scenario 4 update */
          if available po_mstr then do:
          assign v_existpo = yes.
          if po_stat = "C" or po_stat = "x" then 
          assign v_poopen = no.
          else 
          assign v_poopen = yes.
          end.
          else do:
          assign v_existpo  = no
                 v_poopen = yes.
          end.
           
          /* scheduled order */
          if ttpomstr.po_sched then 
          do:
          put stream f_cim unformatted v_quote ttpomstr.po_nbr  v_quote v_space       /* po number */
                                       v_quote ttpomstr.po_vend v_quote v_space skip. /* vendor */
          put stream f_cim unformatted /* start date and end date need to assign use interface data */
                                       v_quote po_start v_quote v_space               /* start effective date */
                                       v_default v_space                              /* end   effective date */
                                       v_default v_space                              /* taxable  */
                                       v_default v_space                              /* taxclass  */                                                                   
                                       v_default v_space                              /* credit terms */
                                       v_quote ttpomstr.po_site v_quote v_space       /* bill-to address BMPT same as site code*/
                                       v_quote ttpomstr.po_site v_quote v_space       /* ship-to address BMPT same as site code*/
                                       v_quote NO v_quote v_space                     /* Print Schedules */
                                       v_quote NO v_quote v_space                     /* EDI Schedules */
                                       v_quote NO v_quote v_space                     /* FAX Schedules */
                                       v_quote ttpomstr.po_site v_quote v_space       /* AP SITE BMPT same as site code*/
                                       v_default v_space                              /*  Zero Schedules */
                                       v_default v_space                              /*  ship via */
                                       v_default v_space                              /*  FOB */
                                       v_default v_space                              /*  Buyer */
                                       v_default v_space                              /*  contact */
                                       v_default v_space           .                  /*  contract */
        if not v_existpo then                                                           
        put stream f_cim unformatted   v_default v_space .                            /*  CURRENTY */
        put stream f_cim unformatted   v_quote NO v_quote v_space                     /*  Fixed Price */
                                       v_quote 0 v_quote v_space                      /*  Order Revision */
                                       v_default v_space                              /*  Order Rev Date */
                                       v_default v_space                              /*  Print PO */
                                       v_default v_space                              /*  Daybook Set */
                                       v_quote NO v_quote v_space                     /*  Comments */
                                       skip.
          put stream f_cim unformatted v_quote NO v_quote v_space  skip.              /*  Import/Export */                
      if v_existpo then           
          put stream f_cim unformatted v_quote NO v_quote v_space  skip.              /* Is this an order revision change */
          put stream f_cim unformatted v_default skip.                                /* Tax infor */
          put stream f_cim unformatted v_default  v_space                             /* Ship Delivery Pattern Code */        
                                       v_default  skip.                               /* Ship Delivery Time Code */        
                                                                   
          /* scheduled line information */                                                   
          for each ttpoddet where ttpoddet.pod_nbr = ttpomstr.po_nbr
                              and ttpoddet.pod_domain = global_domain
                              and ttpoddet.pod_site  = po_site
                              and ttpoddet.pod_dataid = ttpomstr.po_dataid.
                                                  
          find pod_det no-lock where     pod_det.pod_domain = global_domain 
                                 and   pod_det.pod_nbr = ttpoddet.pod_nbr
                                 and   pod_det.pod_part = ttpoddet.pod_part
                                 and   pod_det.pod_site = ttpoddet.pod_site
                                 and   pod_det.pod_line = ttpoddet.pod_line
                                 no-error.                                                           
      
          if available pod_det then assign v_existpod = yes. 
          else assign v_existpod = no.
      
          put stream f_cim unformatted v_quote ttpoddet.pod_part v_quote v_space       /*  Item number */                
                                       v_quote ttpoddet.pod_site v_quote v_space       /*  Site        */
                                       v_quote ttpoddet.pod_line  v_quote               /*  line        */
                                       skip.
          /* if new pod line */                                                   
          if not v_existpod then 
          put stream f_cim unformatted v_quote No v_quote v_space  skip.            /*  Copy from  */        
          put stream f_cim unformatted v_quote ttpomstr.po_pr_list v_quote v_space  /*  Discount Table BMPT is supplier code + site*/
                                       v_default  v_space                           /* unit cost */
                                       v_default  v_space                           /* pur account */
                                       v_default  v_space                           /* sub account */
                                       v_default  v_space                           /* cost center */
                                       v_default  v_space                           /* taxable */
                                       v_default  v_space                           /* tax class */
                                       v_quote ttpoddet.pod_type v_quote v_space    /* po type */
                                       v_default  v_space                           /* Item Revision */
                                       v_default  v_space                           /* Item Rev Date */
                                       v_default  v_space                           /* Update Current Cost */
                                       v_default  v_space                           /* Location BMPT default from 1.4.16*/
                                       v_quote NO v_quote v_space                   /* Fixed Price NO */
                                       v_quote ttpoddet.pod_um v_quote v_space      /* Unit of Measure */
                                       skip.
             
      put stream f_cim unformatted v_default v_space skip                        /* Tax infor */
                                       v_quote ttpoddet.pod_um_conv v_quote v_space 
                                       skip.                                         /* UM conversion */
       /* if po type is subcontract */
                 if ttpoddet.pod_type = "S" then
                           put stream f_cim unformatted v_default  v_space
                           skip.
       
         put stream f_cim unformatted v_default  v_space                            /* Firm Days */
                                      v_default  v_space                            /* Std Pack Qty */
                                      v_default  v_space                            /* Max Order Qty */
                                      v_default  v_space                            /* Cum Start */
                                      v_default  v_space                            /* Fab auth days */
                                      v_default  v_space                            /* Raw Auth Days */
                                      v_default  v_space                            /* Safety Days */
                                      v_default  v_space                            /* Supplier Item */
                                      v_default  v_space                            /* Container Item */
                                      v_default  v_space                            /* Ship Delivery Pattern Code */
                                      v_default  v_space                            /* Ship Delivery Time Code */
                                      v_default  v_space                            /* Transport Days */                                                 
                                       ttpoddet.pod_start  v_space                   /* Start Effective */
                                       ttpoddet.pod_expire v_space                   /* End Effective */
                                       v_quote NO v_quote v_space                    /* Comments */
                                       skip.
                                                                   
         put stream f_cim unformatted  v_default  v_space                            /* Schedule Days */
                                       v_default  v_space                            /* Schedule Weeks */
                                       v_default  v_space                            /* Schedule Months */
                                       skip.
                                                                   
          end. /* for each ttpoddet */
          put stream f_cim unformatted "." skip.
          end. /* when scheduled order */
          
          /* discrete order */
          else do:
          put stream f_cim unformatted v_quote ttpomstr.po_nbr  v_quote v_space skip. /* po number */
          put stream f_cim unformatted v_quote ttpomstr.po_vend v_quote v_space skip. /* vendor */
          put stream f_cim unformatted v_quote ttpomstr.po_ship v_quote v_space skip. /* Ship-to */
          put stream f_cim unformatted v_quote ttpomstr.po_ord_date v_quote v_space   /* order date */
                                       v_quote ttpomstr.po_due_date v_quote v_space   /* Due date */
                                       v_quote ttpomstr.po_buyer v_quote v_space      /* Buyer    */
                                       v_quote ttpomstr.po_bill v_quote v_space       /* Bill to */
                                       v_default v_space                              /* Sales Job */
                                       v_default v_space                              /* Contract */
                                       v_default v_space                              /* Contact */
                                       v_default v_space                              /* Remarks */
                                       v_default v_space                              /* Price tbl */
                                       v_quote ttpomstr.po_pr_list v_quote v_space    /* Disc Tbl */
                                       v_default v_space                              /* Ln Disc */
                                       v_quote ttpomstr.po_site    v_quote v_space    /* site*/
                                       v_default v_space                              /* Daybook Set */
                                       v_default v_space                              /* Project */
                                       v_quote   Yes    v_quote v_space               /* Confirming */
                                       v_quote   no     v_quote v_space               /* Imp/exp */
                                       .
        if not v_existpo then                                                            
        put stream f_cim unformatted   v_default v_space                              /* Currency */
                                       v_default v_space   .                          /* LANGUAGE */
        put stream f_cim unformatted   v_default v_space                              /* Taxable */
                                       v_default v_space                              /* Tax Class */
                                       v_default v_space                              /* Tax Date */
                                       v_quote   no     v_quote v_space               /* Fix price */
                                       v_default v_space                              /* Credit Term */
                                       v_default v_space                              /* Credit Term int */
                                       v_default v_space                              /* REQUEST by */
                                       v_quote   no     v_quote v_space               /* Comments */
                                       skip.
         put stream f_cim unformatted  v_default v_space                              /* Tax Usage */
                                       v_default v_space                              /* Tax Environment */
                                       v_default v_space                              /* Tax Class */
                                       v_default v_space                              /* Taxable */
                                       v_quote   no     v_quote v_space               /* Tax in */
                                       skip.
        /* Discrete Po line detail */
        for each ttpoddet where ttpoddet.pod_nbr = ttpomstr.po_nbr
                            // and ttpoddet.pod_domain = global_domain
                            // and ttpoddet.pod_site  = po_site
                            and ttpoddet.pod_dataid = ttpomstr.po_dataid.
         
         vc_cip = no.
         if can-find(first code_mstr where code_domain = global_domain
                                             and code_fldname = "xxcip"
                                             and code_value begins "glcode"
                                             and code_cmmt = ttpoddet.pod_acct  )                                           
                                             then vc_cip = yes.
              
               
         find pod_det no-lock where pod_det.pod_domain = global_domain 
                                and pod_det.pod_nbr = ttpoddet.pod_nbr
                                and pod_det.pod_part = ttpoddet.pod_part
                                and pod_det.pod_site = ttpoddet.pod_site
                                and pod_det.pod_line = ttpoddet.pod_line
                               no-error.                                                           
      
         if available pod_det then 
            assign v_existpod = yes. 
         else 
            assign v_existpod = no.                                                                   
         put stream f_cim unformatted  v_quote ttpoddet.pod_line v_quote v_space skip.       /* Po line */
         put stream f_cim unformatted  v_quote ttpoddet.pod_site v_quote v_space skip.       /* Po site */
         if not v_existpod then                                                              
         put stream f_cim unformatted  v_quote ttpoddet.pod_part v_quote v_space skip.       /* Po Part */
         if not v_existpod then                                                              
         put stream f_cim unformatted  v_quote ttpoddet.pod_qty_ord v_quote v_space          /* Quantity order */         
                                       v_quote ttpoddet.pod_um v_quote skip.                 /* UM */
         if v_existpod then                                                                  
         put stream f_cim unformatted  v_quote ttpoddet.pod_qty_ord v_quote v_space          /* Quantity order */         
                                        skip.                                                /* UM */
                                                                        
         /* if use price list */
         put stream f_cim unformatted  v_default v_space      skip .                         /* Po cost */                 
         put stream f_cim unformatted  v_default v_space                                     /* SINGLE lot */
                                       v_quote ttpoddet.pod_loc v_quote v_space              /* Location */
                                      //  v_default v_space                                     /* location */ /* scp don't give location code */
                                       v_default v_space                                     /* Item Revision */
                                       v_default v_space                                     /* Item Revision date */
                                       v_default v_space                                     /* STATUS */
                                       v_default v_space                                     /* Supplier Item */
                                       .
        if ttpoddet.pod_type = "M" then 
        put stream f_cim unformatted   v_quote  ttpoddet.pod_desc v_quote  v_space.
        put stream f_cim unformatted
                                       v_default v_space                                     /* Due Date */
                                       v_default v_space                                     /* Performance Date */
                                       v_default v_space                                     /* Need date */
                                       v_default v_space                                     /* So job */
                                       v_quote no v_quote v_space                            /* Fixed Price */
                                       v_quote  ttpoddet.pod_acct v_quote v_space .            /* purchase account  */
       /*                                
       if vc_cip then
         put stream f_cim unformatted  skip v_quote  ttpoddet.pod_cipid v_quote  v_space skip. 
         */  
         put stream f_cim unformatted 
                                       v_quote  ttpoddet.pod_sub v_quote v_space             /* sub acct */
                                       v_quote  ttpoddet.pod_cc v_quote v_space              /* costcentre  */
                                       v_quote  ttpoddet.pod_project v_quote   v_space       /* project */
                                       
                                       v_quote ttpoddet.pod_type v_quote v_space             /* Type */
                                       v_quote ttpoddet.pod_taxable v_quote v_space          /* Tax able */
                                       v_default v_space                                     /* Tax Class */
                                       v_default v_space                                     /* Inspect required */
                                       v_quote no v_quote v_space   .                        /* Comment */
        if not v_existpod then 
        put stream f_cim unformatted   v_quote ttpoddet.pod_um_conv v_quote v_space .        /* Conversion */
        put stream f_cim unformatted   v_default v_space                                     /* Update current cost */
                                       skip.
         if ttpoddet.pod_taxable then 
         put stream f_cim unformatted  v_default v_space                                     /* Tax Usage */
                                       v_default v_space                                     /* Tax Environment */
                                       v_default v_space                                     /* Tax Class */
                                       v_default v_space                                     /* Taxable */
                                       v_quote   no     v_quote v_space                      /* Tax in */
                                       skip.                                                                                                                                                                                 
         if ttpoddet.pod_type = "S" then 
         put stream f_cim unformatted  v_default v_space                                     /* Work order */
                                      v_default v_space                                     /* ID */
                                      v_default v_space                                     /* Operation */
                                      v_default v_space                                     /* Subcontract type*/
                                    
                                      skip.                 
          end. /* for each ttpoddet where ttpoddet.pod_nbr = ttpomstr.po_nbr discrete po detail */
          
          put stream f_cim unformatted  "."      skip.                                        /* SINGLE */                                                       
          put stream f_cim unformatted  "."      skip.                                        /* Tax detail */                                                              
          put stream f_cim unformatted  v_default v_space          skip .                     /* trail screen */           
       if v_existpo then 
          put stream f_cim unformatted  v_default v_space      skip                               
                                        v_quote No v_quote         skip .                     /* Is this an order revision change */  
          if not v_poopen then 
          put stream f_cim unformatted  v_quote No v_quote         skip .                     /* Do you wish to reopen all PO line items */
          put stream f_cim unformatted  "."      skip.                                        /* end */
         
         end. /* else do when discrete po */

         
    if last-of(ttpomstr.po_dataid) then do:

         output stream f_cim close.
          

         if ttpomstr.po_sched then 
            v_runproagram = "rspomt.p".
         if not ttpomstr.po_sched  then  
            v_runproagram = "popomt.p".         
            execname = "xxcrpoapi.p".
            put unformatted v_cim " " skip
                            v_log " " skip 
                            m_outpath " " skip
                            m_archpath " " skip
                            v_runproagram " " skip
                            v_errstring " " skip 
                            v_errorstatus skip .
                            
         run cimloadprocess(input v_cim,
                            input v_log,
                            input v_runproagram,
                            input ttpomstr.po_traceid,
                            input ttpomstr.po_dataid,
                            output v_errstring,
                            output v_errorstatus
                            ) .
         /* To-be add cip id logic */ 
                            
       v_toterrorstatus = v_toterrorstatus and v_errorstatus.    


            /* Handle cim and log file */
        do vi_filecount = 1 to 2:
          /* when success move cim log file to archive directory  */
          /* when failed move cim log file to error directory  */   
          /*  move status json file to out directory for API return */   
          /* when success move previous step .p and json file to archive directory  */
          /* when failed move previous step .p and json file to error directory  */       
          case vi_filecount:
          when 1 then
          assign vc_filename = v_cim
                 vc_topath   = if v_errorstatus then m_errorpath else m_archpath.
          when 2 then
          assign vc_filename = v_log
                 vc_topath   = if v_errorstatus then m_errorpath else m_archpath.
           end case.           
          run cimfilehandle(input vc_filename,
                            input vc_topath,
                            input no ).                 
                 
       end. /* do vi_filecount = 1 to 2: */   
     end. /* if last-of */
     
    end. /* for each ttpomstr  */
     if  v_toterrorstatus then 
                 undo cimloadprocess, leave cimloadprocess.          
            
        
    end.    /* do transaction on error undo cimloadprocess, leave cimloadprocess: */
    
    end. /* if vc_import */
    
       if can-find(first api_status where api_status.return_status = "FAILED" ) then
       do:
       
       for each api_status where api_status.return_status = "SUCCESS".
         
         delete api_status.
         
       end.
       
       end.  /* if can-find(first api_status */
         

        /* Handle cimload related files */
         /* Create output json */                   
         run writejsonfile(input hresult,
                  input vc_outputjson,
                  input "FILE",
                  input True,
                  input "UTF-8",
                  output vl_RetOK,
                  output vc_errorstring).
                  
         put unformatted v_cim " " skip
                         v_log " " skip 
                         m_outpath " " skip
                         m_archpath " " skip
                         v_runproagram " " skip
                         v_errstring " " skip 
                         v_errorstatus skip .
       do vi_filecount = 1 to 5:
          /* when success move cim log file to archive directory  */
          /* when failed move cim log file to error directory  */   
          /*  move status json file to out directory for API return */   
          /* when success move previous step .p and json file to archive directory  */
          /* when failed move previous step .p and json file to error directory  */       
          case vi_filecount:
      
          when 1 then
          assign vc_filename = replace(vc_inputjson,".json",".p")
                 vc_topath   = if v_errorstatus then m_errorpath else m_archpath. 
          when 2 then
          assign vc_filename = vc_inputjson
                 vc_topath   = if v_errorstatus then m_errorpath else m_archpath. 
          when 3 then
          assign vc_filename = replace(vc_inputjson,".json",".dat")
                 vc_topath   = if v_errorstatus then m_errorpath else m_archpath. 
          when 4 then
          assign vc_filename = replace(vc_inputjson,".json",".run.log")
                 vc_topath   = m_outpath.
          when 5 then
          assign vc_filename = replace(vc_inputjson,".json",".run.out.json")
                 vc_topath   =  m_outpath.
          end case.
          
         if vl_RetOK then 
         put unformatted vc_filename " " skip
         vc_topath " " skip 
         vi_filecount skip.
     
        
          
          run cimfilehandle(input vc_filename,
                            input vc_topath,
                            input no ).                 
                   
          
     
       end.       /* do vi_filecount = 1 to 5: */        
       
       output close.   
  

   
   

   /*
   for each ttpomstr, each ttpoddet where ttpoddet.pod_nbr = ttpomstr.po_nbr.
   
   put    unformatted
          ttpomstr.po_nbr       ","
          ttpoddet.pod_dataid   ","
          ttpomstr.po_dataid    ","
          ttpomstr.po_vend      ","
          ttpomstr.po_site      ","
          ttpomstr.po_domain    ","
          ttpomstr.po_ship      ","
          ttpomstr.po_ord_date  ","
          ttpomstr.po_due_date  ","
          ttpomstr.po_buyer     ","
          ttpomstr.po_contact   ","
          ttpomstr.po_pr_list2  ","
          ttpomstr.po_pr_list   ","
          ttpomstr.po_confirm   ","
          ttpomstr.po_sched     ","
          ttpomstr.po_type      ","
          ttpoddet.pod_line     ","
          ttpoddet.pod_site     ","
          ttpoddet.pod_part     ","
          ttpoddet.pod_qty_ord  ","
          ttpoddet.pod_um       ","
          ttpoddet.pod_type     ","
          ttpoddet.pod_wo_lot   ","
          ttpoddet.pod_op       ","
          ttpoddet.pod_fix_pr   ","
          ttpomstr.po_error      
          skip.                  
                                                                                  
   end.
   
  output close. 
   */

  leave .
  
end. /* repeat on error undo mainloop, retry mainloop: */



